package cate.game.role.res.guild.po.my;

import cate.common.table.d.GDGuild;
import cate.common.util.GameResult;
import cate.game.role.RoleDependent;
import cate.game.role.res.guild.msg.apply.MyGuildApplyResp;
import cate.game.role.res.guild.po.GuildPO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;
import cate.game.role.Role;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@NoteClass(value = "我的公会申请")
public class MyGuildApplyPO extends RoleDependent {
    @NoteField(value = "申请公会详情")
    public List<MyGuildApplyDetailPO> list;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (list == null) {
            list = new ArrayList<>();
        }
    }

    @Override
    public void onEnter() {
        super.onEnter();
        checkExpire();
    }

    /**
     * 删除过期的申请数据
     */
    @JsonIgnore
    public void checkExpire() {
        // 玩家身上的申请记录12小时后清掉
        Iterator<MyGuildApplyDetailPO> it = list.iterator();
        while (it.hasNext()) {
            MyGuildApplyDetailPO apply = it.next();
            long hour = (System.currentTimeMillis() - apply.applyTime) / (1000 * 60 * 60);
            if (hour >= GDGuild.basic.APPLY_KEEP_HOUR) {
                it.remove();
            } else {
                // 公会被解散或者公会申请列表里没有你了
                GuildPO guild = role.getGame().res.guild.getGuild(apply.guildIdHex);
                if (guild == null || !guild.apply.exist(role.getUid())) {
                    it.remove();
                }
            }
        }
    }

    public void noticeUpdate() {
        role.sendNow(new MyGuildApplyResp(this));
    }

    /**
     * 申请新公会检查
     */
    @JsonIgnore
    public GameResult<Void> checkApply(String guildId) {
        checkExpire();
        GameResult<Void> r = new GameResult<>();
        if (list.size() >= GDGuild.basic.APPLY_GUILD_NUM_MAX) {
            role.getGame().notice.message(role, "同一时间的公会申请数量超过上限");
            return r.fail("同一时间的公会申请数量超过上限");
        }
        for (MyGuildApplyDetailPO applyPo : role.getRes().guild.apply.list) {
            if (StringUtils.equals(applyPo.guildIdHex, guildId)) {
                role.getGame().notice.message(role, "你已经申请过该公会");
                return r.fail("你已经申请过该公会");
            }
        }
        GuildPO guild = role.getGame().res.guild.getGuild(guildId);
        if (guild == null) {
            role.getGame().notice.message(role, "公会不存在或者已经解散");
            return r.fail("公会不存在或者已经解散");
        }
        if (guild.basic.memberNum >= guild.basic.memberLimit) {
            role.getGame().notice.message(role, "公会人数已达上限");
            return r.fail("公会人数已达上限");
        }
        if (role.getBase().power < guild.basic.powerRequire) {
            role.getGame().notice.message(role, "申请入会战力不够");
            return r.fail("申请入会战力不够");
        }
        if (role.getBase().level < guild.basic.level) {
            role.getGame().notice.message(role, "申请入会等级不够");
            return r.fail("申请入会等级不够");
        }
        return r.success();
    }

    /**
     * 增加申请记录
     */
    public void addApply(MyGuildApplyDetailPO apply) {
        list.add(apply);
        noticeUpdate();
    }

    public void deleteApply(String guildIdHex) {
        list.removeIf(a -> StringUtils.equals(a.guildIdHex, guildIdHex));
        noticeUpdate();
    }

    /**
     * 加入公会后删除申请记录
     */
    public void deleteAllApply() {
        for (MyGuildApplyDetailPO apply : list) {
            GuildPO guild = role.getGame().res.guild.getGuild(apply.guildIdHex);
            if (guild != null) {
                guild.apply.clearSingleApply(role.getGame(), role.getUid());
            }
        }
        this.list.clear();
    }
}
